#include <iostream>
#include <queue>
#include <list>

class MyStack {
public:
    MyStack() {

    }
    
    void push(int x) {
        queue1.push(x);
    }
    
    int pop() {
        if(!queue1.empty()) {
            while(!queue1.empty()) {
                if(queue1.size() == 1) {
                    int val = queue1.front();
                    queue1.pop();
                    return val;
                } else {
                    int val = queue1.front();
                    queue1.pop();
                    queue2.push(val);
                }
            }
        } else if (!queue2.empty()) {
            while(!queue2.empty()) {
                if(queue2.size() == 1) {
                    int val = queue2.front();
                    queue2.pop();
                    return val;
                } else {
                    int val = queue2.front();
                    queue2.pop();
                    queue1.push(val);
                }
            }
        } else {
            return NULL;
        }
    }
    
    int top() {
        if(!queue1.empty()) {
            while(!queue1.empty()) {
                if(queue1.size() == 1) {
                    int val = queue1.front();
                    queue1.pop();
                    queue2.push(val);
                    return val;
                } else {
                    int val = queue1.front();
                    queue1.pop();
                    queue2.push(val);
                }
            }
        } else if (!queue2.empty()) {
            while(!queue2.empty()) {
                if(queue2.size() == 1) {
                    int val = queue2.front();
                    queue2.pop();
                    queue1.push(val);
                    return val;
                } else {
                    int val = queue2.front();
                    queue2.pop();
                    queue1.push(val);
                }
            }
        } else {
            return NULL;
        }
    }
    
    bool empty() {
        return queue1.empty() && queue2.empty();
    }

    std::queue<int, std::list<int>> queue1, queue2;
};

int main(int argc, char** argv)
{


    return 0;
}